package grpc

import (
	"flag"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/health"
	"google.golang.org/grpc/health/grpc_health_v1"
	"log"
	"net"
	"online-medical-consultation/common/nacos"
)

func RegisterGrpc(call func(server *grpc.Server)) {
	nac := nacos.NacosConfig.Grpc
	flag.Parse()
	lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", nac.Host, nac.Port))
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	call(s)
	grpc_health_v1.RegisterHealthServer(s, health.NewServer())
	log.Printf("server listening at %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
